using Microsoft.AspNetCore.Mvc;
using Infrastructure.Filters;
using ZR.Infrastructure;
using ZR.Service.BaseFrame.IService;
using ZR.Model.BaseFrame.Dto;
using ZR.Model.BaseFrame.Entity;
using ZR.Model;
using ZR.Admin.WebApi.MainModules;
using ZR.ServiceCore.Attributes;

namespace ZR.Admin.WebApi.Controllers.BaseFrame
{
    /// <summary>
    /// 公司
    /// </summary>
    public class CompanyControllerBase : ApiControllerBase
    {

        /// <summary>
        /// 公司接口
        /// </summary>
        protected readonly ICompanyService _CompanyService;

        protected readonly MapsterAdaptEntityCfg _mapsterAdaptEntityCfg;

        public CompanyControllerBase(ICompanyService companyService, MapsterAdaptEntityCfg mapsterAdaptEntityCfg)
        {
            _CompanyService = companyService;
            _mapsterAdaptEntityCfg = mapsterAdaptEntityCfg;
        }

        /// <summary>
        /// 添加公司
        /// </summary>
        /// <returns></returns>
        [HttpPost("add")]
        [ActionPermissionFilter(Permission = "company:add")]
        [Log(Title = "公司", BusinessType = BusinessType.INSERT)]
        public virtual async Task AddCompany([FromBody] CompanyFormDto form)
        {
            await CheckFormParam(form);
            var model = form.Adapt<CompanyEntity>(_mapsterAdaptEntityCfg).ToCreate(HttpContext, EntityExtension.AutoFillField2);
            var isOk = await _CompanyService.AddCompany(model);
            if (!isOk)
            {
                throw new CustomException(ResultCode.FAIL, "添加失败");
            }
        }

        /// <summary>
        /// 修改公司
        /// </summary>
        /// <returns></returns>
        [HttpPost("modify")]
        [ActionPermissionFilter(Permission = "company:edit")]
        [Log(Title = "公司", BusinessType = BusinessType.UPDATE)]
        public virtual async Task ModifyCompany([FromBody] CompanyFormDto form)
        {
            await CheckFormParam(form);
            var model = form.Adapt<CompanyEntity>(_mapsterAdaptEntityCfg).ToUpdate(HttpContext);
            var isOk = await _CompanyService.ModifyCompany(model);
            if (!isOk)
            {
                throw new CustomException(ResultCode.FAIL, "更新失败");
            }
        }

        /// <summary>
        /// 删除公司
        /// </summary>
        /// <returns></returns>
        [HttpPost("delete")]
        [ActionPermissionFilter(Permission = "company:delete")]
        [Log(Title = "公司", BusinessType = BusinessType.DELETE)]
        public virtual async Task DeleteCompany([FromBody] DeleteCompanyReq req)
        {
            var idArr = Tools.SplitAndConvert<long>(req.Ids);
            var isOk = await _CompanyService.DeleteCompany(idArr, false);
            if (!isOk)
            {
                throw new CustomException(ResultCode.FAIL, "删除失败");
            }
        }

        /// <summary>
        /// 变更公司状态
        /// </summary>
        /// <returns></returns>
        [HttpPost("changeState")]
        [ActionPermissionFilter(Permission = "company:changestate")]
        [Log(Title = "公司", BusinessType = BusinessType.UPDATE)]
        public virtual async Task ChangeCompanyState([FromBody] ChangeCompanyStateReq req)
        {
            var isOk = await _CompanyService.ChangeCompanyState(req);
            if (!isOk)
            {
                throw new CustomException(ResultCode.FAIL, "更新失败");
            }
        }




        /// <summary>
        /// 查询公司树选择列表
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost("treeSelectList")]
        [ActionPermissionFilter(Permission = "company:treeSelectList")]
        public virtual async Task<List<TreeNodeInfo>> GetCompanyTreeSelectList([FromBody] GetCompanyTreeSelectListReq req)
        {
            var result = await _CompanyService.GetCompanyTreeSelectList(req);
            return result;
        }


        /// <summary>
        /// 查询公司分页列表
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost("pageList")]
        [ActionPermissionFilter(Permission = "company:list")]
        public virtual async Task<PagedInfo<CompanyListItemDto>> GetCompanyPageList([FromBody] GetCompanyPageListReq req)
        {
            var result = await _CompanyService.GetCompanyPageList(req);
            return result;
        }

        /// <summary>
        /// 查询公司详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("detail/{id}")]
        [ActionPermissionFilter(Permission = "company:detail")]
        public virtual async Task<CompanyDetailDto> GetCompanyDetail(long id)
        {
            var result = await _CompanyService.GetCompanyDetail(id);
            return result;
        }

    }
}